using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Chess
{
    public class CoTuong_Control : ControlSuper
    {
        private static QuanCoTuong[] DSQuanCoTuosngKhoiTao = { QuanCoTuong.XeDen, QuanCoTuong.MaDen, QuanCoTuong.TuojngDen, QuanCoTuong.SiDen, QuanCoTuong.TuosngDen, QuanCoTuong.SiDen, QuanCoTuong.TuojngDen, QuanCoTuong.MaDen, QuanCoTuong.XeDen };

        public override void KhoiTao(int qcDiTruoc)
        {
            bc = new BanCoTuong();
            _laCo = 1;
            _ktBanCo.X = 234;
            _ktBanCo.Y = 260;
            _ktOCo = 26;
            _soOCo = 9;
            _csHinhNext = 17;
            bc.KhoiTaoBanCo(qcDiTruoc);
            dsQC.Clear();
            bc.DanhSachQuanCo(ref dsQC);
            qcDuocPhepDi = qcDiTruoc;
            _thTren = true;
        }

        public override void LoadTrangThai(byte[] dl)
        {
            bc = new BanCoTuong();
            _laCo = 1;
            _ktBanCo.X = 234;
            _ktBanCo.Y = 260;
            _ktOCo = 26;
            _soOCo = 9;
            _csHinhNext = 17;

            bc.LoadBanCo(dl);
            dsQC.Clear();
            bc.DanhSachQuanCo(ref dsQC);
            qcDuocPhepDi = (int)dl[0];
            _thTren = true;
        }

        public override Hashtable[] DiMotNuocCo(int vtDau, int vtCuoi)
        {
            Hashtable[] kq = new Hashtable[2];
            kq[0] = new Hashtable();
            kq[1] = new Hashtable();
            QuanCoTuong temp1 = QuanCoTuong.OTrong;
            QuanCoTuong temp2 = QuanCoTuong.OTrong;

            if (!dsQC.ContainsKey(vtDau))
                return null;

            temp1 = (QuanCoTuong)dsQC[vtDau];
            dsQC.Remove(vtDau);
            if (dsQC.ContainsKey(vtCuoi))
            {
                temp2 = (QuanCoTuong)dsQC[vtCuoi];
                dsQC.Remove(vtCuoi);
            }

            foreach (int each in dsQC.Keys)
            {
                kq[0].Add(each, dsQC[each]);
            }
            kq[1].Add(vtDau, temp1);
            kq[1].Add(vtCuoi, temp2);
            int[] kqDiCo = bc.DiMotNuocCo(vtDau, vtCuoi);
            if (kqDiCo != null)
            {
                dsQC.Add(vtCuoi, temp1);
                qcDuocPhepDi = (qcDuocPhepDi == 0) ? 1 : 0;
            }
            else
            {
                dsQC.Add(vtDau, temp1);
                if (temp2 != QuanCoTuong.OTrong)
                    dsQC.Add(vtCuoi, temp2);
                return null;
            }

            return kq;
        }
    }
}
